package com.wdzr.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wdzr.model.Project;
import com.wdzr.service.ProjectService;

@Controller
@RequestMapping("/project")

public class ProjectController {

    @Autowired
    private ProjectService projectService;  

    /**
     *跳转到添加项目界面
     */
    @RequestMapping("/toAddProject")
    @ResponseBody
    public String toAddProject(){
        return "暂无页面";
    }
    
    /**
     * 添加项目并重定向  
     * @param model
     * @param project
     * @return
     */
    @RequestMapping("/addProject") 
    @ResponseBody
    public Project addProject(Model model,Project project){
    	if(projectService.saveProject(project)){
    		project = projectService.findProjectById(project.getId());  
             model.addAttribute("project", project);
        }
        return project;
    }

    /**
     * 修改项目
     * @param model
     * @param request
     * @param project
     * @return
     */
    @RequestMapping("/updateProject")  
    @ResponseBody
    public Project UpdateProject(Model model,Project project){
             if(!projectService.updateProject(project)){  
            	 project = projectService.findProjectById(project.getId());  
                    model.addAttribute("project", project);  
                }
             return project;
    }

    /**
     * 查询所有项目
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getAllProject")  
    @ResponseBody
    public List<Project> getAllProject(Model model){
    	List<Project> project = projectService.findAll();
        model.addAttribute("projectList",project);
        return project;
    }

    /**
     * 查询单个项目
     * @param id
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getProject") 
    @ResponseBody
    public Project getProject(int id,Model model){
    	Project project=projectService.findProjectById(id);
        model.addAttribute("project",project);  
        return project;
    }
    /**
     * 根据id删除项目
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/delProject") 
    @ResponseBody
    public String deleteProject(int id,Model model){
        model.addAttribute("project", projectService.deleteProject(id));  
        return "删除完成";
    }

}
